/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: DealerUserControllerImpl
 * Author:   Allen
 * Date:     2020/8/27 15:28
 * Description: 商家/系统用户信息处理实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.rest.impl;

import com.bim.marvel.openapi.client.dto.ResourceCenterDTO;
import com.bim.marvel.openapi.client.dto.ResourceCenterNotDescriptionDTO;
import com.bim.marvel.openapi.client.query.ResourceCenterQuery;
import com.bim.marvel.common.core.constant.CodeEnum;
import com.bim.marvel.common.core.model.dto.SimpleVO;
import com.bim.marvel.common.util.SimpleConverter;
import com.bim.marvel.openapi.core.entity.ResourceCenterDO;
import com.bim.marvel.openapi.core.service.impl.ResourceCenterService;
import com.bim.marvel.rest.ResourceCenterController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 〈商家/系统用户信息处理实现类〉
 *
 * @author Allen
 * @date 2020/8/27
 * @since 1.0.0
 */
@RestController
public class ResourceCenterControllerImpl implements ResourceCenterController {

    /**
     * 注入ResourceCenter
     */
    @Autowired
    private ResourceCenterService resourceCenterService;


    @Override
    @CacheEvict(value = "getResourceCenterTree", allEntries = true)
    public SimpleVO<Boolean> saveResourceCenter(ResourceCenterQuery resourceCenterQuery) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = resourceCenterService.saveOrUpdateResourceCenter(resourceCenterQuery);
        if (!success) {
            simpleVO = new SimpleVO(CodeEnum.INSERT_FAILED);
        }
        return simpleVO;
    }

    @Override
    @CacheEvict(value = "getResourceCenterTree", allEntries = true)
    public SimpleVO<Boolean> updateResourceCenter(ResourceCenterQuery resourceCenterQuery) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = resourceCenterService.saveOrUpdateResourceCenter(resourceCenterQuery);
        if (!success) {
            simpleVO = new SimpleVO(CodeEnum.UPDATE_FAILED);
        }
        return simpleVO;
    }

    @Override
    @Cacheable(value = "getResourceCenterTree")
    public SimpleVO<List<ResourceCenterNotDescriptionDTO>> getResourceCenterList() {
        List<ResourceCenterDO> resourceCenterDOList = resourceCenterService.list();
        if (CollectionUtils.isEmpty(resourceCenterDOList)) {
            return new SimpleVO<>(CodeEnum.DATA_NOT_FOUND);
        }
        List<ResourceCenterNotDescriptionDTO> resourceCenterDTOList = SimpleConverter.convert(resourceCenterDOList,
                ResourceCenterNotDescriptionDTO.class);
        return new SimpleVO<>(resourceCenterDTOList);
    }

    @Override
    public SimpleVO<ResourceCenterDTO> getAppDetail(String id) {
        ResourceCenterDO resourceCenterDO = resourceCenterService.getById(id);
        if (resourceCenterDO != null) {
            ResourceCenterDTO resourceCenterDTO = new ResourceCenterDTO();
            BeanUtils.copyProperties(resourceCenterDO, resourceCenterDTO);
            return new SimpleVO<>(resourceCenterDTO);
        }
        return new SimpleVO(CodeEnum.DATA_NOT_FOUND);
    }
}
